package com.example.lenovo.zxing_lxy.utils.permission;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;

import com.example.lenovo.zxing_lxy.utils.ToastUtil;
import com.joker.api.Permissions4M;
import com.joker.api.wrapper.ListenerWrapper;
import com.joker.api.wrapper.Wrapper;

/**
 * Created by lenovo on 2017/10/25.
 */

public class Permissionhelp {

    public static final int REQ_CODE_PERMISSION = 1;



    public static void getPermission(final Activity mActivity, String permission, int code, final PermissionCallback permissionCallback){
        Permissions4M.get(mActivity)
                .requestPermissions(permission)
                .requestCodes(code)
                .requestListener(new ListenerWrapper.PermissionRequestListener() {
                    @Override
                    public void permissionGranted(int code) {
                        permissionCallback.onSuccessful();
                    }

                    @Override
                    public void permissionDenied(int code) {
                        showObtainPrompt(code);

                    }
                    @Override
                    public void permissionRationale(int code) {
                        ToastUtil.show("请打开使用相机权限权限 in activity with listener");
                    }
                })
                .requestPageType(Permissions4M.PageType.MANAGER_PAGE)
                .requestPage(new Wrapper.PermissionPageListener() {
                    @Override
                    public void pageIntent(int code, final Intent intent) {
                        new AlertDialog.Builder(mActivity)
                                .setMessage(getDialogMessage(code))
                                .setPositiveButton("前往设置页面", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        mActivity.startActivity(intent);
                                    }
                                })
                                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        dialog.dismiss();
                                    }
                                })
                                .show();
                    }
                })
                .request();
    }


    private static String getDialogMessage(int code){
        String message = "";
        switch (code)
        {
            case REQ_CODE_PERMISSION:
                message= "用户您好，我们需要您开启读使用相机权限权限申请：\n请点击前往设置页面\n(in activity with" +
                        " listener)";
                break;

        }
        return message;
    }


    private static void showObtainPrompt(int code) {
        switch (code)
        {
            case REQ_CODE_PERMISSION:
                ToastUtil.show("获取使用相机权限失败 in activity with listener");
                break;
        }


    }

}
